import axios from 'axios'
import { Message } from 'element-ui'
import store from '@/store'
import router from '@/router'

// create an axios instance
const service = axios.create({
  baseURL: process.env.VUE_APP_BASE_API, // url = base url + request url
  // withCredentials: true, // send cookies when cross-domain requests
  timeout: 5000 // request timeout
})

// 请求拦截器
service.interceptors.request.use(config => {
  const { token } = store.getters
  if (token) {
    store.dispatch('user/logout')
    router.push('/login')
  }
  return config
}, error => {
  return Promise.reject(error)
}
)

export default service
